﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Antlr.StringTemplate;

#endregion

namespace DusonFramework.TemplateEngine
{
    /// <summary>
    /// The service of template
    /// </summary>
    public sealed class TemplateService
    {
        /// <summary>
        /// Generates the specified contract.
        /// </summary>
        /// <param name="contract">The contract.</param>
        /// <returns></returns>
        public static string Generate(TemplateContract contract, string culture)
        {
            // get template file path
            HttpContext context = HttpContext.Current;
            if (context == null)
                throw new Exception("Current httpcontext is null");
            string path = context.Server.MapPath(String.Format("~/App_Data/Templates/{0}", culture));

            // create the template instance
            StringTemplateGroup templateGroup = new StringTemplateGroup("template", path);
            StringTemplate template = templateGroup.GetInstanceOf(contract.Name);

            foreach (var para in contract.Parameters)
            {
                template.SetAttribute(para.Key, para.Value);
            }

            string a = template.ToString();
            return template.ToString();
        }
    }
}
